package com.li.utils;

import android.content.Context;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/4/14
 * Description: popupWindow包含头部、左右键
 */

public class ConfirmPopWindow extends PopupWindow {
    private TextView mTxtHead;
    private TextView mTxtLeft;
    private TextView mTxtRight;
    private View mView;

    public void setTouchOutsideDismiss(boolean touchOutsideDismiss) {
        mIsTouchOutsideDismiss = touchOutsideDismiss;
    }

    private boolean mIsTouchOutsideDismiss=true;

    /**
     *
     * @param widthPer  宽度所占占比
     * @param heightPer 高度所占占比
     * @param context   上下文
     */
    public ConfirmPopWindow(float widthPer, float heightPer, Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.layout_confirm_pop, null);
        mTxtHead= (TextView) mView.findViewById(R.id.txt_head);
        mTxtLeft= (TextView) mView.findViewById(R.id.txt_left);
        mTxtRight= (TextView) mView.findViewById(R.id.txt_right);
        LinearLayout layoutContent = (LinearLayout) mView.findViewById(R.id.layout_content);
        setWidth(-1);
        setHeight(-1);
        setContentView(mView);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        ViewGroup.LayoutParams params= layoutContent.getLayoutParams();
        Point point=ScreenUtil.getScreenSize(context);
        params.width= (int) (point.x*1.0f*widthPer);
        params.height= (int) (point.y*1.0f*heightPer);

            mTxtLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mConfirmClickListener != null) {
                        mConfirmClickListener.onNegativeClick(v);

                    }
                }
            });
            mTxtRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mConfirmClickListener != null) {
                        mConfirmClickListener.onPositiveClick(v);
                    }
                }
            });

    }

    public ConfirmPopWindow setHeadText(String txt){
        mTxtHead.setText(txt);
        return this;
    }
    public ConfirmPopWindow setPositiveButton(String txt, View.OnClickListener onClickListener){
        mTxtRight.setText(txt);
        mTxtRight.setOnClickListener(onClickListener);
        return this;
    }
    public ConfirmPopWindow setNegativeButton(String txt, View.OnClickListener onClickListener){
        mTxtLeft.setText(txt);
        mTxtLeft.setOnClickListener(onClickListener);
        return this;
    }

    public ConfirmPopWindow setPositiveText(String text){
        mTxtRight.setText(text);
        return this;
    }
    public ConfirmPopWindow setNegativeText(String text){
        mTxtLeft.setText(text);
        return this;
    }

    public void setConfirmClickListener(OnConfirmClickListener confirmClickListener) {
        mConfirmClickListener = confirmClickListener;
    }

    private OnConfirmClickListener mConfirmClickListener;


    public interface OnConfirmClickListener{
        void onPositiveClick(View view);
        void onNegativeClick(View view);
    }

}
